package com.baihan.angus.app.game.busshow.ui.user.album;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.basic.utils.Loog;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.arouter_path.ArouterAppPath;
import com.cjt2325.cameralibrary.JCameraView;
import com.cjt2325.cameralibrary.listener.ErrorListener;
import com.cjt2325.cameralibrary.listener.JCameraListener;

import java.io.File;

import static com.baihan.angus.app.game.basic.utils.AppUtil.APP_FILE_SAVE_PATH;

@Route(path = ArouterAppPath.CAMERA_ACTIVITY)
public class CameraActivity extends AppCompatActivity {

    private JCameraView jcameraview;
    private String finalPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        //注册
        ARouter.getInstance().inject(this);
        View decorView = getWindow().getDecorView();
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        } else {
            int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(option);
        }

        jcameraview = findViewById(R.id.jcameraview);

        //设置视频保存路径
        jcameraview.setSaveVideoPath(APP_FILE_SAVE_PATH);
        //设置视频质量
        jcameraview.setMediaQuality(JCameraView.MEDIA_QUALITY_MIDDLE);

        //JCameraView监听
        jcameraview.setErrorLisenter(new ErrorListener() {
            @Override
            public void onError() {
                //打开Camera失败回调
                Loog.i("CJT", "open camera error");
            }

            @Override
            public void AudioPermissionError() {
                //没有录取权限回调
                Loog.i("CJT", "AudioPermissionError");
            }
        });

        jcameraview.setJCameraLisenter(new JCameraListener() {
            @Override
            public void captureSuccess(Bitmap bitmap) {
                //获取图片bitmap
                String fileName = System.currentTimeMillis() + ".jpg";
                AppUtil.saveImageToGallery(CameraActivity.this, bitmap, fileName);
                finalPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "spoiler/" + fileName;

                Intent intent = new Intent();
                intent.putExtra("path", finalPath);
                setResult(RESULT_OK, intent);
                finish();
                Loog.i("JCameraView", "bitmap = " + bitmap.getWidth());
            }

            @Override
            public void recordSuccess(String url, Bitmap firstFrame) {
                //获取视频路径
                finalPath = url;

                Intent intent = new Intent();
                intent.putExtra("path", finalPath);
                setResult(RESULT_OK, intent);
                finish();
                Loog.i("CJT", "url = " + url);
            }
        });
        //左边按钮点击事件
        jcameraview.setLeftClickListener(() -> {
            CameraActivity.this.finish();
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        jcameraview.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        jcameraview.onPause();
    }
}